package com.shinhye.mobilelottery.net.protocol;

import java.io.StringWriter;

import org.xmlpull.v1.XmlSerializer;

import android.util.Xml;

import com.shinhye.mobilelottery.ConstantValue;

public class Message {
	private Header header;
	private Body body;

	public Message() {
		header = new Header();
		body = new Body();
	}

	/**
	 * 序列化Message
	 * @param serializer
	 */
	public void serializer(XmlSerializer serializer) {
		try {
			serializer.startTag(null, "message");
			serializer.attribute(null, "version", "1.0");
			Element element = body.getElements().get(0);
			header.getTransactiontype().setValue(
					element.getTransactiontype());
			header.serializer(serializer, body.getBody());
			// body.serializer(serializer);
			serializer.startTag(null, "body");
			serializer.text(body.getBodyDES());
			serializer.endTag(null, "body");
			serializer.endTag(null, "message");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String getXml(Element element) {
		body.getElements().add(element);
		XmlSerializer serializer = Xml.newSerializer();
		StringWriter writer = new StringWriter();
		try {
			serializer.setOutput(writer);
			serializer.startDocument(ConstantValue.ENCODING, null);
			
			this.serializer(serializer);
			
			serializer.endDocument();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return writer.toString();
	}
	
	public Header getHeader() {
		return header;
	}

	public Body getBody() {
		return body;
	}
}
